<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><![endif]-->
    <title>天量科技管理后台</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <meta name="viewport" content="width=device-width">
    <link href="favicon.ico" rel="shortcut icon" type="image/x-icon">
    <link rel="stylesheet" href="css/templatemo_main.css">
    <link href="//netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body ng-app="app" ng-controller="index">
<div class="navbar navbar-inverse" role="navigation">
    <div class="navbar-header">
        <div class="logo"><h1>天量科技管理后台</h1></div>
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
    </div>
</div>
<div class="template-page-wrapper">
    <div class="navbar-collapse collapse templatemo-sidebar">
        <ul class="templatemo-sidebar-menu">
            <li>
                <form class="navbar-form">
                    欢迎您:{{user.name || '访客'}}</span>
                </form>
            </li>
            <li ng-repeat="menu in menus" ng-class="menu.menuclass" ng-click="menuclick(menu)">
                <a ng-click="redi(menu.url)" ng-if="menu.items.length==0"><i ng-class="menu.icon"></i>{{menu.label}}</a>
                <a href="#" ng-if="menu.items.length>0">
                    <i ng-class="menu.icon"></i> {{menu.label}} <div class="pull-right"><span class="caret"></span></div>
                </a>
                <ul class="templatemo-submenu" ng-if="menu.items.length>0">
                    <li ng-repeat="item in menu.items" ng-click="redi(item.url)"><a href="javascript:void(0)">{{item.label}}</a></li>
                </ul>
            </li>
            <li>
                <a href="#" data-toggle="modal" data-target="#confirmModal"><i class="fa fa-sign-out"></i>退出登录</a>
            </li>
        </ul>
    </div><!--/.navbar-collapse -->

    <div class="templatemo-content-wrapper">
        <div class="templatemo-content">
            <ol class="breadcrumb">
                <li><a ng-click="redi('index.html')" href="javascript:void(0)">首页</a></li>
                <li><a ng-click="redi('forex_config.html?type=Series')" href="javascript:void(0)">返回</a></li>
            </ol>
            <h4 ng-if="!haserror">编辑[<font color="red">{{config.id}}</font>]号加仓数列:[<font color="red">{{config.name}}</font>]
                <button class="btn btn-mini btn-success" ng-click="update()">全部更新</button>
            </h4>

            <div class="row" ng-if="haserror">
                <div class="alert alert-danger alert-dismissible" role="alert">
                    <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <strong>发生异常情况:{{errormessage}}</strong>
                </div>
            </div>

            <div class="panel panel-primary">
                <table class="table table-striped table-hover table-bordered">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>浮亏线</th>
                        <th>盈利线</th>
                        <th>加仓点位</th>
                        <th>回调点位</th>
                        <th>仓位</th>
                        <th>暂停时间</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr ng-repeat="item in list track by $index">
                        <td>{{item.i}}</td>
                        <td><input type="number" class="form-control form_items_md form_items"  ng-model="item.sl" required></td>
                        <td><input type="number" class="form-control form_items_md form_items"  ng-model="item.tp" required></td>
                        <td><input type="number" class="form-control form_items_md form_items"  ng-model="item.addpip" required></td>
                        <td><input type="number" class="form-control form_items_md form_items"  ng-model="item.backpip" required></td>
                        <td><input type="number" class="form-control form_items_md form_items"  ng-model="item.prs" required></td>
                        <td><input type="number" class="form-control form_items_md form_items"  ng-model="item.time" required></td>
                    </tr>
                    </tbody>
                </table>
            </div>

        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="confirmModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="myModalLabel">确定退出吗</h4>
                </div>
                <div class="modal-footer">
                    <a ng-click="logout()" class="btn btn-primary" data-dismiss="modal">确定</a>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>

    <footer class="templatemo-footer">
        <div class="templatemo-copyright">
            <p>Copyright &copy; 2018 By <a href="http://skyquant.cn" title="天量科技" target="_blank">天量科技</a></p>
        </div>
    </footer>
</div>
</body>

<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<script src="//cdn.bootcss.com/angular.js/1.5.8/angular.js"></script>
<script src="//cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="js/common.js"></script>
<script src="js/templatemo_script.js"></script>
<script type="text/javascript">
  'use strict';
  var app = angular.module('app', []);
  var maxsize = 31;
  app.config(['$locationProvider', function ($locationProvider) {
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });
  }]);
  app.controller('index', ['$scope', '$http','$location', function ($scope, $http,$location) {

      //每个页面都要有的方法begin
      $scope.init = function(){
        var id = $location.search().id;
        if(id=='' || id==null || id=='undefined'){
           $scope.haserror = true;
           $scope.errormessage = '参数不正确，请从货币组页面点击编辑按钮进入';
           return;
        }
        $scope.configid = id;
        getUser($http,$scope,'货币组','后台参数设置');
      }

      $scope.logout = function(){
        logout($http);
      }

      $scope.menuclick=function(menu){
        changemenuclass($scope,menu);
      }

      $scope.redi=function(url){
          location.href = url;
      }
      //每个页面都要有的方法begin

      //每个页面都需要重写此方法，发生在init之后
      $scope.initAfter = function(){
         $http.get('quant4j-service/forex/config/'+$scope.configid).success(function(data,header,config,status) {
            if(data.ok){
                $scope.config = data.data;
            }
         }).error(function(data,header,config,status){
         });
         $http.get('quant4j-service/forex/config/series?configId='+$scope.configid).success(function(data,header,config,status) {
            if(data.ok){
                $scope.num = Object.keys(data.data).length;
                var list = new Array(maxsize);
                for(var i=0;i<list.length;i++){
                    list[i] = {
                        "i":i,
                        "tp":0,
                        "sl":0,
                        "addpip":0,
                        "backpip":0,
                        "time":0,
                        "prs":0};
                }
                var i =0;
                angular.forEach(data.data,function(s){
                    if(i<maxsize){
                        list[i++] = s;
                    }
                });
                $scope.list = list;
                //$scope.config = data.data.config;
            }
        }).error(function(data,header,config,status){
        });
      }

      $scope.update = function(){
        var list = $scope.list;
        var str = "";
        var errornum = 0;
        angular.forEach(list,function(d){
            if(d!=null){
                if(typeof(d.sl) != "undefined"
                    && typeof(d.tp) != "undefined"
                    && typeof(d.prs) != "undefined"
                    && typeof(d.time) != "undefined"
                    && typeof(d.addpip) != "undefined"
                    && typeof(d.backpip) != "undefined"){
                    str += d.sl+"_"+d.tp+"_"+d.addpip+"_"+d.backpip+"_"+d.prs+"_"+d.time+";";
                }else{
                    errornum++;
                }
            }
        });
        if(errornum>0){
            alert("加仓数列配置中有输入框未填写数字，请检查");
            return;
        }

        if(str!=""){
            var dataMap = {"str":str,"configId":$scope.configid};
            $http({
                method:'POST',
                url:'quant4j-service/forex/config/series',
                data:dataMap,
                headers:{'Content-Type': 'application/x-www-form-urlencoded'},
                 transformRequest: function(obj) {
                     var s = [];
                     for(var p in obj){
                         s.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                     }
                     return s.join("&");
                 }
            }).success(function(req){
                if(req.ok){
                    alert("更新加仓数列成功");
                    $scope.initAfter();
                }else{
                    alert(req.message);
                }
            });
        }else{
            alert("更新未成功,请检查参数");
        }
      }
      $scope.init();
  }]);
</script>
</html>